package com.gexiaobao.pigeon.ui.fragment.mine.banking.activity

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.bin.david.form.core.SmartTable
import com.bin.david.form.data.style.FontStyle
import com.bin.david.form.data.style.LineStyle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.BankLimitResponse
import com.gexiaobao.pigeon.databinding.ActivityBankLimitBinding
import com.gexiaobao.pigeon.viewmodel.MineBankingViewModel
import java.lang.StringBuilder

/**
 * @Author      : hxw
 * @Date        : 2024/3/28 16:09
 * @Describe    : 限额提醒
 */
class ActivityBankLimit : BaseActivity<MineBankingViewModel, ActivityBankLimitBinding>() {

    lateinit var data: SmartTable<BankLimitResponse.ListBean>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("限额说明") {
            finish()
        }

        mDatabind.swipeRefreshLayout.init {
            refreshData()
        }
        initSmartTable()
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }


    private fun initSmartTable() {
        data = mDatabind.smartTable as SmartTable<BankLimitResponse.ListBean>
        val fontStyle = FontStyle(this, 18, ContextCompat.getColor(this, R.color.black))
        val contentStyle = FontStyle(this, 14, ContextCompat.getColor(this, R.color.comm_text_gray))
        val lineStyle = LineStyle()
        lineStyle.color = ContextCompat.getColor(this, R.color.dedede)
        data.config.columnTitleStyle = fontStyle
        data.config.contentStyle = contentStyle
        data.config.setHorizontalPadding(10)
            .setVerticalPadding(30)
            .setSequenceHorizontalPadding(10)
            .setSequenceVerticalPadding(30)
            .setShowTableTitle(false) // 去除标题
            .setFixedFirstColumn(true) //第一行作废
            .setShowXSequence(false)
            .setShowYSequence(false)
            .isFixedYSequence = true
    }

    private fun refreshData() {
        showLoading(getString(R.string.loading))
        mViewModel.getBankLimit()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.bankLimitResult.observe(this) {
            dismissLoading()
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            setSmartTableData(it)
        }
    }

    private fun setSmartTableData(it: BankLimitResponse) {
        val listData = ArrayList<BankLimitResponse.ListBean>()
        if (!it.isEmpty()) {
            it.list.forEachIndexed { _, listBean ->
                listData.add(listBean)
                val bankName = listBean.bankName
                val djk = listBean.djk
                val jjk = listBean.jjk
                if (bankName.isNotEmpty()) {
                    if (bankName.length > 5) {
                        listBean.bankName = bankName.substring(0, 4) + "\n" + bankName.substring(5, bankName.length)
                    }
                }
                if (djk.isNotEmpty()) {
                    if (djk.contains("，")) {
                        splitContent(2, listBean, djk, "，")
                    } else if (djk.contains("；")) {
                        splitContent(2, listBean, djk, "；")
                    } else if (djk.contains(",")) {
                        splitContent(2, listBean, djk, ",")
                    } else if (djk.contains("：")) {
                        splitContent(2, listBean, djk, "：")
                    }
                }

                if (jjk.isNotEmpty()) {
                    if (jjk.contains("，")) {
                        splitContent(1, listBean, jjk, "，")
                    } else if (jjk.contains("；")) {
                        splitContent(1, listBean, jjk, "；")
                    } else if (jjk.contains(",")) {
                        splitContent(1, listBean, jjk, ",")
                    } else if (jjk.contains("：")) {
                        splitContent(1, listBean, jjk, "：")
                    }
                }
            }
            data.setData(it.list)
        }
    }

    private fun splitContent(type: Int, listBean: BankLimitResponse.ListBean, bankType: String, split: String) {
        val sb = StringBuilder()
        val result = bankType.split(split)
        result.forEachIndexed { _, s ->
            sb.append(s + "\n")
        }
        if (type == 1) {
            listBean.jjk = sb.toString().trim()
        } else {
            listBean.djk = sb.toString().trim()
        }
    }
}